<template>
  <div>
    <attach-upload
      :tags="uploadTags"
      :parent-tag-search="true"
      :parent-lable="parentTag"
      :view-mode="viewMode"
      :batch-no="queryId"
      v-bind="$listeners"
      @after-data="handleAfterData"
    >
      <template slot="header">
        <h3 class="attach-title">附件资料</h3>
        <hr />
      </template>
    </attach-upload>
  </div>
</template>

<script>
import AttachUpload from '@/bank/components/attach/Upload'
const parentTag = '代偿附件'
export const enumPersonalTags = [
  { tag: '委托担保合同原件', required: true, parentTag },
  { tag: '借款合同复印件', required: true, parentTag },
  { tag: '身份证复印件', required: true, parentTag },
  { tag: '借据（借款凭证）复印件', required: true, parentTag },
  { tag: '客户资信表（或尽调报告）复印件', required: true, parentTag },
  { tag: '证明征信情况的相关资料', required: true, parentTag },
  { tag: '其他资料', required: false, parentTag },
]
export const enumCompanyTags = [
  { tag: '委托担保合同原件', required: true, parentTag },
  { tag: '借款合同复印件', required: true, parentTag },
  { tag: '身份证复印件', required: true, parentTag },
  { tag: '借据（借款凭证）复印件', required: true, parentTag },
  { tag: '客户资信表（或尽调报告）复印件', required: true, parentTag },
  { tag: '证明征信情况的相关资料', required: true, parentTag },
  { tag: '法定代表人及实际控制人连带责任承诺函原件', required: true, parentTag },
  { tag: '以及相关佐证资料', required: true, parentTag },
  { tag: '其他资料', required: false, parentTag },
]
export default {
  components: {
    AttachUpload,
  },
  props: {
    queryId: String,
    viewMode: { type: Boolean, default: true },
  },
  data() {
    return {
      parentTag,
      loading: false,
      projectData: {},
      attachs: [],
      tableName: 'datum_one_county_industry',
    }
  },
  computed: {
    isCompany() {
      return +this.projectData.customer_type === 1
    },
    uploadTags() {
      return this.isCompany ? enumCompanyTags : enumPersonalTags
    },
  },
  watch: {
    queryId: {
      handler: function () {
        this.getData()
      },
      immediate: true,
    },
  },
  created() {},
  mounted() {},
  methods: {
    handleClose() {
      this.$emit('close')
    },
    getData() {
      this.loading = true
      this.$api
        .getData(this.$serviceName, this.tableName, { 'customer_related_id.eq': this.queryId })
        .then(res => {
          this.projectData = res?.data?.[0] ?? {}
          this.isCorp = this.projectData.is_corp_con || '1'
        })
        .finally(() => {
          this.loading = false
        })
    },
    handleAfterData({ list, emit }) {
      this.attachs = list

      if (emit) {
        this.updateStatus()
      }
    },

    // 更新项目附件状态
    updateStatus() {
      // 判断附件状态
      const listObj = this.attachs.reduce((prev, curr) => {
        const tag = curr.attachment_type
        if (!prev[tag]) prev[tag] = []
        prev[tag].push(curr)
        return prev
      }, {})
      const isAllUpload = this.uploadTags.every(tag => {
        if (!tag.required) return true
        const atts = listObj[tag.tag]
        return atts && atts.length > 0
      })
      const currentStatus = isAllUpload ? 1 : 0
      const attachStatus = +this.projectData.is_dc_attach_complete
      if (attachStatus === currentStatus) {
        return this.$emit('after-data', {})
      }

      // 更新状态
      this.$api
        .updateData({
          table: 'datum_one_county_industry',
          id: this.queryId,
          data: {
            is_dc_attach_complete: currentStatus,
          },
        })
        .then(() => {
          // 观察：更改 props 对象值
          this.projectData.is_dc_attach_complete = currentStatus
        })
        .finally(() => {
          this.$emit('status', '')
        })
    },
  },
}
</script>

<style lang="scss" scoped>
.attach-title {
  font-size: 1.1rem;
  font-weight: bold;
}
</style>
